package m.jclecai.com.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gongwen.marqueen.MarqueeView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.BannerAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.AdvertBean;
import m.jclecai.com.bean.HomeItem;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;
import m.jclecai.com.view.CircleFlowIndicator;
import m.jclecai.com.view.NoticeMFView;
import m.jclecai.com.view.ViewFlow;

/**
 * Created by andy on 2017/8/4.
 * <p>
 * 这里是最新的home页面；
 */
public class HomeFragment_ extends BaseFragment {


    private List<AdvertBean> listData = new ArrayList<>();



    @BindView(R.id.viewflow)
    ViewFlow mViewflow;

    @BindView(R.id.viewflowindic)
    CircleFlowIndicator mViewflowindic;


    //走马灯
    @BindView(R.id.marqueeView)
    MarqueeView mMarqueeView;
    //数字彩
    @BindView(R.id.rv_num_lottery)
    RecyclerView mRvNumLottery;
    //高频彩
    @BindView(R.id.rv_heigh_frequncy_lottery)
    RecyclerView mRvHeighFrequncyLottery;
    //足彩
    @BindView(R.id.rv_sport_lottery)
    RecyclerView mRvSportLottery;
    //自动刷新
    @BindView(R.id.sr_refresh)
    AutoSwipeRefreshLayout mSrRefresh;

    @BindView(R.id.tv_new_user)
    TextView mTvNewUser;

    @BindView(R.id.tv_guest_grade)
    TextView mTvGuestGrade;
    private List<HomeItem> mSportLotteryList;
    private List<HomeItem> mNumberLotteryList;
    private List<HomeItem> mHeighFrequencyLotteryList;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_;
    }

    @Override
    public void initView() {
        super.initView();

        mSrRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getBannerData();
                showMarpuee();
            }
        });
        String text1 = " <font size='2' color='red'>15元红包新手礼</font>\n <br/>" +
                "        <font size='1' >新手红包任务等你完成<font>";

        String text2 = "\n" +
                "        <font size='2' color='red'>猜比分赢万元彩金</font>\n <br/>" +
                "        <font size='1' >免费参与大奖天天浪<font>";

        mTvNewUser.setText(Html.fromHtml(text1));
        mTvGuestGrade.setText(Html.fromHtml(text2));
    }


    @Override
    public void initData() {

        mSportLotteryList = new ArrayList<>();
        mNumberLotteryList = new ArrayList<>();
        mHeighFrequencyLotteryList = new ArrayList<>();

        mSportLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mSportLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mSportLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mSportLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;


        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;

        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mNumberLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;


        mHeighFrequencyLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mHeighFrequencyLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mHeighFrequencyLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;
        mHeighFrequencyLotteryList.add(new HomeItem("福彩3d",R.drawable.types_3d,"好运连连","fc3d")) ;


        getBannerData();
        showMarpuee();

        showNumLottery();
        showHeighFrequencyLottery();
        showSporterLottery();

    }

    //显示足彩
    private void showSporterLottery() {

        mRvSportLottery.setLayoutManager(new GridLayoutManager(getContext(),4){

            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvSportLottery.setAdapter(new LotteryAdapter(mSportLotteryList));

    }
    //高频彩票
    private void showHeighFrequencyLottery() {

        mRvHeighFrequncyLottery.setLayoutManager(new GridLayoutManager(getContext(),4){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvHeighFrequncyLottery.setAdapter(new LotteryAdapter(mHeighFrequencyLotteryList));

    }
    //数字彩票
    private void showNumLottery() {
        mRvNumLottery.setLayoutManager(new GridLayoutManager(getContext(),4){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvNumLottery.setAdapter(new LotteryAdapter(mNumberLotteryList));
    }

    @Override
    public void initListener() {
        super.initListener();

    }



    void getBannerData() {
        HttpManager.getAsyn(Constant_Url.ADVERT_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                if (mSrRefresh != null)
                    mSrRefresh.setRefreshing(false);
            }

            @Override
            public void onResponse(String response) {
                if (mSrRefresh != null)
                    mSrRefresh.setRefreshing(false);
                RecordsBean recordsBean = FastOk.get(response, RecordsBean.class);
                if (recordsBean == null) return;
                if (recordsBean.getRecords() == null) return;
                listData.clear();
                listData.addAll(recordsBean.getRecords());
                if (listData != null && listData.size() > 0) {
                    initBanner();
                }
            }
        });
    }



    private void initBanner() {
        if (mViewflow == null) return;
        mViewflow.setNestedpParent((ViewGroup) mViewflow.getParent());
        mViewflow.setAdapter(new BannerAdapter(getActivity(), listData).setInfiniteLoop(true));
        mViewflow.setmSideBuffer(listData.size()); //实际图片张数，
        mViewflowindic.setCount(listData.size());
        mViewflow.setFlowIndicator(mViewflowindic);
        mViewflow.setTimeSpan(4500);
        mViewflow.setSelection(listData.size() * 1000); // 设置初始位置
        mViewflow.startAutoFlowTimer(); // 启动自动播放
    }



    void showMarpuee() {
        HttpManager.getAsyn(Constant_Url.MARQUEE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                mMarqueeView.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {
                    JSONObject parse = FastOk.get(response, JSONObject.class);
                    if (parse != null && "0".equals(parse.getString("status"))) {
                        JSONArray records = parse.getJSONArray("records");
                        List<CharSequence> info = new ArrayList<>();
                        for (int i = 0; i < records.size(); i++) {
                            info.add(records.getJSONObject(i).getString("title"));
                        }
                        if (info.size() > 0 && mMarqueeView != null)
                            mMarqueeView.setVisibility(View.VISIBLE);
                        if (mMarqueeView != null) {
                            NoticeMFView noticeMFView = new NoticeMFView(getActivity());
                            noticeMFView.setData(info);
                            mMarqueeView.setMarqueeFactory(noticeMFView);
                            mMarqueeView.startFlipping();
                        }

                    }
                }
            }
        });
    }

    public class  LotteryAdapter extends BaseRecyclerViewAdapter<HomeItem>{

        public LotteryAdapter(List<HomeItem> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.home_lottery_item;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, HomeItem bean, int position) {

        }
    }

}

